package com.youlu.campus.web.community.controller;

import com.youlu.campus.entity.VO.req.CheckMobileVO;
import com.youlu.campus.entity.VO.req.ItemCheckStockReq;
import com.youlu.campus.entity.VO.req.QaCommitCommand;
import com.youlu.campus.entity.VO.req.QaInfoReq;
import com.youlu.campus.entity.english.vo.request.CompetitionCaCollectRequest;
import com.youlu.campus.service.activity.UserParticipateActivityInfoService;
import com.youlu.campus.service.activity.WechatMiniQaService;
import com.youlu.campus.service.ca.UserCaService;
import com.youlu.campus.service.order.MobileBindOrderService;
import com.youlu.campus.service.platform.PlatformTaskService;
import com.youlu.campus.service.universitytimetable.UserSignInService;
import com.youlu.campus.service.wechat.WXacodeService;
import com.youlu.campus.web.common.base.RequestContext;
import com.youlu.campus.web.ca.service.UserCaInfoService;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 微信小程序活动控制器
 */
@RestController
public class WechatMiniController {

    @Autowired
    private WechatMiniQaService wechatMiniQaService;

    @Autowired
    private UserCaService userCaService;

    @Autowired
    private UserParticipateActivityInfoService userParticipateActivityInfoService;

    @Autowired
    private WXacodeService wXacodeService;

    @Autowired
    private MobileBindOrderService mobileBindOrderService;

    @Autowired
    private PlatformTaskService platformTaskService;

    @Autowired
    private UserCaInfoService userCaInfoService;

    @Autowired
    private UserSignInService userSignInService;

    /**
     * 检测QA答题次数是否满足
     * @param qaInfoReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/check/qa-count")
    public QResult checkQaCount(@RequestBody @Validated QaInfoReq qaInfoReq, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        wechatMiniQaService.checkUserQaLimit(RequestContext.getCurUser(),qaInfoReq.getActivityId(),qaInfoReq.getQaInfoId());
        return QResult.success(null);
    }

    /**
     * 积分兑换答题次数
     * @param qaInfoReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/points-exchange/qa-count")
    public QResult userPointsExchangeQaCount(@RequestBody @Validated QaInfoReq qaInfoReq, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        wechatMiniQaService.userPointsExchangeQaCount(RequestContext.getCurUser(),qaInfoReq.getActivityId(),qaInfoReq.getQaInfoId());
        return QResult.success(null);
    }

    /**
     * 答题提交-微信小程序-答题提交
     *
     * @param
     * @return
     */
    @PutMapping("/mini-app/wechat/user-qa/commit/{qaInfoId}")
    public QResult qaCommit(@PathVariable String qaInfoId, @RequestBody @Validated QaCommitCommand commitCommand,
                            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        commitCommand.setQaInfoId(qaInfoId);
        commitCommand.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(wechatMiniQaService.doQaCommit(commitCommand));
    }


    /**
     * 电子证书领取
     * @param caCollectReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/mini/ca-receive")
    public QResult caCollectMini(@RequestBody @Validated CompetitionCaCollectRequest caCollectReq,
                                   BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setQaReceive(Boolean.TRUE);
        return QResult.success(userCaService.caMiniReceive(caCollectReq));
    }

    /**
     * 电子证书领取
     * @param caCollectReq
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/history/ca-receive")
    public QResult caCollectMiniHistory(@RequestBody @Validated CompetitionCaCollectRequest caCollectReq,
                                 BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        caCollectReq.setCurrentUser(RequestContext.getCurUser());
        caCollectReq.setQaReceive(Boolean.TRUE);
        return QResult.success(userCaService.caReceiveHistory(caCollectReq));
    }

    @GetMapping("/mini-app/ca/list")
    public QResult caUserReceive(){
        return QResult.success(userCaService.userCaList(RequestContext.getCurUser()));
    }

    @GetMapping("/mini-app/ca/qrcode")
    public QResult caReceiveQrcode(){
        return QResult.success(wechatMiniQaService.getUserCaReceiveQrCode(RequestContext.getAppId()));
    }

    /**
     * 初始化活动报名数据
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/init/sign-up")
    public QResult initSignUpByActivityId(@RequestParam String activityId){
        userParticipateActivityInfoService.initSignUpByActivityId(activityId);
        return QResult.success(null);
    }

    /**
     * 获取小程序二维码
     * @param
     * @return
     */
    @GetMapping("/mini-app/wxa/share-code")
    public QResult getShareWxaCode(@RequestParam String activityId) {
        return QResult.success(wXacodeService.getShareCode(RequestContext.getCurUser(),activityId));
    }

    @GetMapping("/public/wxa/code")
    public QResult getWxaCode(String id) {
        return QResult.success(wXacodeService.findById(id));
    }

    @PostMapping("/mini-app/order/query-mobile")
    public QResult queryOrderByMobile(@RequestBody CheckMobileVO checkMobile){
        checkMobile.setUserPO(RequestContext.getCurUser());
        mobileBindOrderService.doBindMobileOrder(checkMobile);
       return QResult.success(null);
    }


    /**
     * 获取公众号和小程序订阅消息配置
     * @param
     * @return
     */
    @GetMapping("/public/platform-sub/config")
    public QResult getS(@RequestParam String appId,@RequestParam String platform) {
        return QResult.success(platformTaskService.findPlatformByAppId(appId,platform));
    }
    @PostMapping("/mini-app/item/check-stock")
    public QResult checkItemStock(@RequestBody ItemCheckStockReq itemCheckStockReq){
        itemCheckStockReq.setCurrentUser(RequestContext.getCurUser());
        return QResult.success(userCaInfoService.checkItemStock(itemCheckStockReq));
    }

    /**
     * 获取打卡活动签到信息
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/user/sign-in")
    public QResult getUserSignInInfo(@RequestParam String activityId){
        return QResult.success(userSignInService.getUserSignInfo(RequestContext.getCurUser(),activityId));
    }


    /**
     * 获取打卡活动邀请展示数据
     * @param activityId
     * @return
     */
    @GetMapping("/mini-app/user/sign-in/invite")
    public QResult getUserSignInInviteInfo(@RequestParam String activityId){
        return QResult.success(userSignInService.getUserSignInviteInfo(RequestContext.getCurUser(),activityId));
    }


}
